package inventory.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import inventory.common.CommonConstants;
import inventory.common.ResultData;
import inventory.entity.MaterialCategory;
import inventory.entity.MaterialTypical;
import inventory.entity.po.MaterialBrandAndModelPo;
import inventory.entity.po.TerminalAndEquipmentPo;
import inventory.service.MaterialCategoryService;
import inventory.service.MaterialTypicalService;
import inventory.service.TypeService;
import inventory.utils.UUIDUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lihang
 */
@Api(tags = "材料类别/类型 增删查改")
@RestController
public class TypeController {

    @Autowired
    private TypeService typeService;

    @Autowired
    private MaterialCategoryService materialCategoryService;

    @ApiOperation("查询管理员管辖的所有材料种类")
    @RequestMapping(value = CommonConstants.TYPE_PREFIX + "/queryTypeByManagerId",method = RequestMethod.GET)
    @ApiImplicitParam(name = "managerId",value = "对应的管理员id", required = true)
    public ResultData queryTypeByManagerId(@RequestParam("managerId") String managerId){
        List<MaterialTypical> materialTypicals =  typeService.queryTypeByManagerId(managerId);
        if (materialTypicals!=null)
            return new ResultData().OK().setResultIns("查询完成").setObjs(materialTypicals);
        else
            return new ResultData().OK().setResultIns("结果为空");
    }

    /**
     * 新增材料类型 接口
     * @param materialTypical
     * @return ResultData
     */
    @RequiresPermissions("material:operate")
    @ApiOperation(value = "新建材料类型")
    @RequestMapping(value = CommonConstants.TYPE_PREFIX + "/addMaterial",method = RequestMethod.POST)
    @ApiImplicitParams({@ApiImplicitParam(name = "brand",value = "品牌",example = "华为"),
            @ApiImplicitParam(name = "categoryId",value = "类别主键（终端为1，装备为2，材料为3）",required = true),
            @ApiImplicitParam(name = "typicalModel",value = "型号",required = true),
            @ApiImplicitParam(name = "typicalName",value = "名称",required = true),
            @ApiImplicitParam(name = "unit",value = "材料单位",example = "台",required = true),
            @ApiImplicitParam(name = "managerId",value = "对应的管理员id",required = true)})
    public ResultData addMaterialCategory(MaterialTypical materialTypical){
        materialTypical.setTypicalId(UUIDUtils.getId());
        typeService.addMaterialType(materialTypical);
        return new ResultData().OK().setResultIns("材料信息插入成功");
    }

    /**
     * 根据id删除材料类型 接口
     * @param typicalId
     * @return ResultData
     */
    @RequiresPermissions("material:operate")
    @ApiOperation(value = "根据id删除材料类型")
    @RequestMapping(value = CommonConstants.TYPE_PREFIX + "/deleteMaterialById",method = RequestMethod.DELETE)
    @ApiImplicitParams({@ApiImplicitParam(name = "typicalId",value = "材料主键",required = true)})
    public ResultData deleteMaterialCategory(@RequestParam("typicalId") String typicalId){
        boolean isDelete = typeService.deleteMaterialTypeById(typicalId);
        if (isDelete)
            return new ResultData().OK().setResultIns("材料信息删除成功");
        else
            return new ResultData().FAILD().setResultIns("该记录仍存在子目录，无法删除");
    }

    /**
     * 根据id批量删除材料类型
     * @param typesId
     * @return ResultData
     */
    @RequiresPermissions("material:operate")
    @ApiOperation(value = "根据id批量删除材料类型")
    @RequestMapping(value = CommonConstants.TYPE_PREFIX+"/deleteMaterialByArray",method = RequestMethod.DELETE)
    @ApiImplicitParams({@ApiImplicitParam(name = "typesId",value = "材料主键数组",allowMultiple = true,required = true)})
    public ResultData deleteMaterialByArray(@RequestParam(value = "typesId") List<String> typesId){
        boolean isDelete = typeService.deleteMaterialByArray(typesId);
        if (isDelete)
            return new ResultData().OK().setResultIns("材料信息删除成功");
        else
            return new ResultData().FAILD().setResultIns("该记录仍存在子目录，无法删除");
    }

    /**
     * 根据id修改材料类型 接口
     * @param materialTypical
     * @return ResultData
     */
    @RequiresPermissions("material:operate")
    @ApiOperation(value = "根据id修改材料类型")
    @RequestMapping(value = CommonConstants.TYPE_PREFIX + "/modifyMaterialById",method = RequestMethod.POST)
    @ApiImplicitParams({@ApiImplicitParam(name = "brand",value = "品牌",required = true),
                        @ApiImplicitParam(name = "categoryId",value = "类别主键（终端为1，装备为2，材料为3）",required = true),
                        @ApiImplicitParam(name = "typicalModel",value = "型号",required = true),
                        @ApiImplicitParam(name = "typicalName",value = "名称",required = true),
                        @ApiImplicitParam(name = "unit",value = "材料单位",example = "台",required = true),
                        @ApiImplicitParam(name = "typicalId",value = "类型")})
    public ResultData modifyMaterialCategory(MaterialTypical materialTypical){
        typeService.modifyMaterialTypeById(materialTypical);
        return new ResultData().OK().setResultIns("材料信息修改成功");
    }

    /**
     * 根据id查询材料类型/类别 接口
     * @param categoryId
     * @param typicalId
     * @param flag
     * @return ResultData
     */
    @ApiOperation(value = "查询单个材料类别(复合接口)")
    @RequestMapping(value = CommonConstants.TYPE_PREFIX + "/queryType",method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "categoryId",value = "材料类别主键"),
                        @ApiImplicitParam(name = "typicalId",value = "材料类型主键"),
                        @ApiImplicitParam(name = "flag",value = "标识 调用类别（category）还是类型（type）接口",required = true)})
    public ResultData selectMaterialCategory(@RequestParam(value = "categoryId", required = false) String categoryId,
                                             @RequestParam(value = "typicalId", required = false) String typicalId,
                                             @RequestParam("flag") String flag){
        //根据标识(flag)选择是类别(category)还是类型(type)
        if (flag.equals("category")) {  //选择类别
            MaterialCategory materialCategoryFind = typeService.selectMaterialCategory(categoryId);

            if (materialCategoryFind == null){
                return new ResultData().FAILD().setResultIns("材料类别信息查询失败");
            }else {
                return new ResultData().OK().setResultIns("材料类别信息查询成功").setObjs(materialCategoryFind);
            }
        }else if (flag.equals("type")){ //选择类型
            MaterialTypical materialTypicalFind = typeService.selectMaterialType(typicalId);

            if (materialTypicalFind == null){
                return new ResultData().FAILD().setResultIns("材料类型信息查询失败");
            }else {
                return new ResultData().OK().setResultIns("材料类型信息查询成功").setObjs(materialTypicalFind);
            }
        }
        return new ResultData().FAILD().setResultIns("参数错误！");
    }

    /**
     * 根据终端或装备名，管理员id查询材料信息
     * @param typicalName
     * @param managerId
     * @return ResultData
     */
    @ApiOperation(value = "根据终端或装备名，管理员id查询材料信息")
    @RequestMapping(value = CommonConstants.TYPE_PREFIX+"/queryTypeByTypicalName",method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "typicalName", value = "材料类型名"),
                        @ApiImplicitParam(name = "managerId",value = "对应的管理员id",required = true)})
    public ResultData queryTypeByTypicalName(@RequestParam("typicalName") String typicalName,
                                             @RequestParam("managerId") String managerId){
        List<TerminalAndEquipmentPo> resultList = typeService.queryTypeByTypicalName(typicalName,managerId);
        if (resultList != null)
            return new ResultData().OK().setObjs(resultList);
        else
            return new ResultData().FAILD().setResultIns("查询结果为空");
    }


    /**
     * 查询城市 接口
     * @return ResultData
     */
    @ApiOperation(value = "查询城市")
    @RequestMapping(value = CommonConstants.TYPE_PREFIX + "/queryCity",method = RequestMethod.GET)
    public ResultData queryCity(){
        List list = typeService.queryCity();
        if (list == null){
            return new ResultData().FAILD().setResultIns("城市查询失败");
        } else {
            return new ResultData().OK().setResultIns("城市查询成功").setObjs(list);
        }
    }

    /**
     * 查询所有县 接口
     * @return ResultData
     */
    @ApiOperation(value = "查询所有县")
    @RequestMapping(value = CommonConstants.TYPE_PREFIX + "/queryCounty",method = RequestMethod.GET)
    public ResultData queryCounty(){
        List list = typeService.queryCounty();
        if (list == null){
            return new ResultData().FAILD().setResultIns("县名查询失败");
        } else {
            return new ResultData().OK().setResultIns("县名查询成功").setObjs(list);
        }
    }

//=============================此代码区域，由sxt添加 ···start··· ===========================================//
    /**
     *查询所有类别
     * @return ResultData
     */
    @ApiOperation("查询所有类别")
    @GetMapping(CommonConstants.TYPE_PREFIX + "/getAllCategory")
    public ResultData getAllCategory(){

        List<MaterialCategory> materialCategories = materialCategoryService.selectList(new EntityWrapper<>());
        return new ResultData().OK().setResultIns("类别目录获取成功").setObjs(materialCategories);
    }

    @Autowired
    private MaterialTypicalService materialTypicalService;

    /**
     *根据查询id查询材料类型
     * @param id
     * @return ResultData
     */
    @ApiOperation("根据查询id查询类型")
    @GetMapping(CommonConstants.TYPE_PREFIX + "/getTypicalByCategoryId/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "材料类型(typical)主键",required = true),
            @ApiImplicitParam(name = "managerId",value = "管理员id",required = true)
    })

    public ResultData getTypicalByCategoryId(@PathVariable("id") String id,String managerId){

        List<MaterialTypical> materialTypicals = materialTypicalService.selectList(new EntityWrapper<MaterialTypical>().eq("category_id", id).and().eq("manager_id", managerId));

        return new ResultData().OK().setResultIns("类别目录获取成功").setObjs(materialTypicals);
    }

    /**
     *根据类别主键获取相应的材料类型名称
     * @param id
     * @return ResultData
     */
    @ApiOperation("根据类别主键获取相应的材料类型名称")
    @GetMapping(CommonConstants.TYPE_PREFIX + "/getTypicalNameByCategoryId/{categoryId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "材料类别(category)主键",required = true),
            @ApiImplicitParam(name = "managerId",value = "管理员id",required = true)
    })
    public ResultData getTypicalNameByCategoryId(@PathVariable("categoryId") String id, String managerId){
        List<String> rs = materialTypicalService.queryTypicalByCategoryId(id,managerId);
        return new ResultData().OK().setObjs(rs);
    }


    /**
     * 获取所有相同名称的材料品牌、材料型号、材料主键
     * @param name
     * @return ResultData
     */
    @ApiOperation("获取所有相同名称的材料品牌、材料型号、材料主键")
    @GetMapping(CommonConstants.TYPE_PREFIX + "/getMaterialBrandAndModelPoByTypicalName/{typicalName}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "typicalName",value = "材料类型名",required = true),
            @ApiImplicitParam(name = "managerId",value = "管理员id",required = true),
            @ApiImplicitParam(name = "categoryId",value = "材料种类id",required = true)
    })
    public ResultData getMaterialBrandAndModelPoByTypicalName(@PathVariable("typicalName") String name,String managerId,String categoryId){
        List<MaterialBrandAndModelPo> materialBrandAndModelPos = materialTypicalService.queryMaterialBrandAndModelPoByTypicalName(name,managerId,categoryId);
        return new ResultData().OK().setObjs(materialBrandAndModelPos);
    }


//=============================此代码区域，由sxt添加 ···end···===========================================//

}
